/**
 * @file
 * @auth zhaojing
 * @date 2016/11/24
 */

import './style.scss';
import tpl from './tpl.html';

import Vue from 'vue';
import modal from '../../../../../grsc-base/component/Modal.vue';
import help from '../../../../../grsc-base/component/Help.vue';
import Popover from '../../../../../grsc-base/component/Popover.vue';
import Validator from '../../../../../grsc-base/component/Validator/main';
import {REG_EMAIL, REG_PHONE,REG_PAYPWD} from '../../../../../grsc-base/common/constants';
import commonUtil from '../../../../../grsc-base/common/commonUtil';

import VerifyCode from '../../../../component/VerifyCode.vue';
import {RULE_ASSET_PASSWORD_LENGTH, RULE_ASSET_PASSWORD_PATTERN, RULE_CODE_SEND} from '../../../../common/contants';
import service from './service';

export default  Vue.extend({
    template: tpl,
    props: {
        onBindingSuccess: {
            type: Function,
            default() {
            }
        }
    },
    components: {
        modal,
        help,
        Popover,
        VerifyCode,
        Validator
    },
    created() {
        this.queryGoogleStatus();
    },
    mounted() {

    },
    computed: {

    },
    data() {
        return {
            form: {
                currentPayPwd: '',
                newPayPwd: '',
                confirmPayPwd:'',
                identifyingCode: ''
            },
            rules: {
                currentPayPwd: {
                    tip:'',
                    required: true,
                    requiredTip: this.$t('common.currentPassRequireTip')
                },
                newPayPwd: {
                    tip:'',
                    required: true,
                    requiredTip: this.$t('common.newPassRequireTip'),
                    minlength: 8,
                    minlengthTip: this.$t('common.ruleAssetPassLength'),
                    maxlength: 20,
                    maxlengthTip: this.$t('common.ruleAssetPassLength'),
                    pattern: REG_PAYPWD,
                    patternTip: this.$t('common.ruleAssetPassPattern')
                },
                confirmPayPwd: {
                    required: true,
                    requiredTip: this.$t('common.repeatPassRequireTip'),
                    equalTo: 'newPayPwd',
                    equalToTip: this.$t('common.passEqualTip')
                },
                identifyingCode: {
                    required: true,
                    requiredTip: this.$t('common.verifyCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.IDENTIFY[this.$i18n.locale]
                }

            },
            isDisabled: true,
            hasSubmitted: false,
            isRequesting: false,
            googleStatus:'OFF'
        }
    },
    watch:{

    },
    methods: {
        onSendSuccess() {
            this.rules.identifyingCode.$setRight(this.$t('common.codeSentTip'));
        },
        onSendFail(rep) {
            this.rules.identifyingCode.$setError(rep.msg);
        },

        queryGoogleStatus(){
            let googleBound = window.localStorage.getItem('GOOGLE_BOUND');
            let googleStatus = window.localStorage.getItem('GOOGLE_STATUS');

            if(googleBound === 'true' && googleStatus === 'ON'){
                this.googleStatus = googleStatus;
                this.form.googleCode = '';
                this.rules.googleCode = {
                    required: true,
                    requiredTip: this.$t('common.googleCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.GOOGLE_CODE[this.$i18n.locale]
                };
            }else{
                this.googleStatus = 'OFF';
            }
        },

        submit(){

            let isCheckAllOk = this.rules.$checkAll();
            if(isCheckAllOk){
                this.isRequesting = true;
                
                let param = {
                    payPassword: this.form.newPayPwd
                };

                let header = {
                    'pay-password': this.form.currentPayPwd,
                    'service-code': this.form.identifyingCode
                };
                if(this.googleStatus === 'ON'){
                    header['google-code'] = this.form.googleCode
                }

                /*
                 currentPayPwd: this.form.currentPayPwd,
                 newPayPwd: this.form.newPayPwd,
                 confirmPayPwd: this.form.confirmPayPwd,
                 identifyingCode: this.form.identifyingCode
                 */
                service.setPayPwd(param, header).done((rep) => {
                    if (rep.code === window.CODE.SUCCESS) {
                        window.success(this.$t('common.success'));
                        this.hide();
                    } else {
                        this.isRequesting = false;
                        if(rep.code === window.CODE.IDENTIFY.CODE){
                            this.rules.identifyingCode.$setError(window.CODE.IDENTIFY[this.$i18n.locale]);
                        } else if (rep.code === window.CODE.IDENTIFY_INVALID.CODE) {
                            this.rules.identifyingCode.$setError(window.CODE.IDENTIFY_INVALID[this.$i18n.locale]);
                        } else if (rep.code === window.CODE.PAY_PWD.CODE && rep.data && rep.data.num) {
                            let num = rep.data.num;
                            if (3 - num > 0) {
                                this.rules.currentPayPwd.$setError(this.$t('common.IncorrectAssetPassTip', {num: 3 - num}));
                            } else {
                                this.rules.currentPayPwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                            }
                        } else if (rep.code === window.CODE.PAY_PWD_LOCK.CODE) {
                            this.rules.currentPayPwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                        } else if (rep.code === window.CODE.GOOGLE_CODE.CODE){
                            this.rules.googleCode.$setError(window.CODE.GOOGLE_CODE[this.$i18n.locale]);
                        }
                    }
                });
            }
        },
        focusTips(){
            this.rules.newPayPwd.tip = this.$t('common.ruleAssetPassLength');
        },
        blurTips(){
            this.rules.newPayPwd.tip = "";
        },

        hide() {
            this.$emit('showAlterPayPwd');
        }
    }

});
